package cn.stylefeng.guns.sys.core.exception;


import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 *  异常处理类
 * </p>
 *
 * @author 落清秋123
 * @since 2020-05-08
 */
public class BizCheckException extends Exception {
	
	public static final String KEY_MESSAGE = "message";

	public static final String KEY_ROOT_CAUSE = "rootCause";

	public static final String KEY_CODE = "code";
	
	
	private static final long serialVersionUID = 720545623505952701L;
	
	public BizCheckException(Map<String, Object> errMap) {
		super();
		this.errMap = errMap;
	}
	
	public BizCheckException(int code, String message) {
		super();
		this.errMap = new HashMap<String, Object>();
		this.code =code;
		this.message =message;
		errMap.put(KEY_CODE, code);
		errMap.put(KEY_MESSAGE, message);
		
	}
	
	public BizCheckException(int code, String message, String... arguments ) {
		super();
		this.errMap = new HashMap<String, Object>();
		errMap.put(KEY_MESSAGE, code);
		if(arguments != null && arguments.length >0) {
			message =MessageFormat.format(message, arguments);
			errMap.put(KEY_MESSAGE,message);
		}else {
			errMap.put(KEY_MESSAGE, message);
		}
		this.code =code;
		this.message =message;
	}
	
	public BizCheckException(BizExceptionEnum bizExceptionEnum ) {
		super();
		this.errMap = new HashMap<String, Object>();
		errMap.put(KEY_CODE, bizExceptionEnum.getErrorCode());
		errMap.put(KEY_MESSAGE,bizExceptionEnum.getMessage());
		this.code =bizExceptionEnum.getErrorCode();
		this.message =bizExceptionEnum.getMessage();
		
	}
	
	
	public BizCheckException(BizExceptionEnum bizExceptionEnum, String... arguments  ) {
		super();
		this.errMap = new HashMap<String, Object>();
		errMap.put(KEY_CODE, bizExceptionEnum.getErrorCode());
		String message =bizExceptionEnum.getMessage();
		if(arguments != null && arguments.length >0) {
			message=MessageFormat.format(bizExceptionEnum.getMessage(), arguments);
			errMap.put(KEY_MESSAGE,message);
		}else {
			errMap.put(KEY_MESSAGE, bizExceptionEnum.getMessage());
		}
		this.code =bizExceptionEnum.getErrorCode();
		this.message =message;
	}

	/**
	 * 功能说明:
	 * 取值范围:
	 * 依赖属性:
	 * 最后修改时间:
	 */
	protected transient Map<String, Object> errMap;
	
	/**
	 * 功能说明:
	 * 取值范围:
	 * 依赖属性:
	 * 最后修改时间:
	 */
	protected transient int code;
	
	
	/**
	 * 功能说明:
	 * 取值范围:
	 * 依赖属性:
	 * 最后修改时间:
	 */
	protected transient String message;

	public Map<String, Object> getErrMap() {
		return errMap;
	}

	public void setErrMap(Map<String, Object> errMap) {
		this.errMap = errMap;
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	
}
